﻿
    $('ul.tab li a').click(function () {
        $('ul.tab li').each(function () {
            $(this).removeClass("tabfocus");
        });
        $(this).parent().addClass("tabfocus");
        var attrVal = $(this).attr("title");
        if (attrVal === 'newsabstract') {
            $('div.news-content').fadeOut();
            $('div.abstract').fadeIn();
        } else if (attrVal === 'newscontent') {
            $('div.abstract').fadeOut();
            $('div.news-content').fadeIn();
        }
    });
$('a.close').click(function () {
    $('#image-url-tiny').val('');
    $('#image-des').val('');
    $('#img-title').val('');
    $('#preview-image img').attr('src','');
    $('div.ef-bg-white').fadeOut();
});
$('.image-browser').click(function () {
    $('div.file-browser').fadeIn();
    loadFolderStructoreTop();
    $('#image-url').val('');
    $('#show-image').attr('src', '');
    $('.crop-image').hide();
});
$('a.close-1').click(function () {
    $('#image-url').val('');
    $('#show-image').attr('src', '');
    $('.crop-image').hide();
    $('div.file-browser').fadeOut();
});



    
    var folderInfo = '/Uploads/images';
var objClicked;
$('span.folder-top a').click(function () {
    folderInfo = "/Uploads/images";
});
function loadFolderStructoreTop() {
    $.ajax({
        url: '/Product/LoadFolerStructureTop',
        success: function (data, textStatus) {
            var listFolder = '';
            var listFile = '';
            var i;
            if (data[0].length > 0) {
                for (i = 0; i < data[0].length; i++) {
                    listFolder += '<li class="directory-collapsed isFolder"><a rel="/Uploads' + data[0][i] + '" href="#" class="key-pc">' + data[0][i].substring(data[0][i].lastIndexOf('/') + 1, data[0][i].length) + '</a></li>';
                }
            }
            if (data[1].length > 0) {
                for (i = 0; i < data[1].length; i++) {
                    listFile += '<li class="file-image"><a rel="/Uploads' + data[1][i] + '" href="#">' + data[1][i].substring(data[1][i].lastIndexOf('/') + 1, data[1][i].length) + '</a></li>';
                }
            }
            $('.sub-folder').html(listFolder + listFile).hide().slideDown('1000');
            $('ul#subsub > li.isFolder > a').on('click', function (e) {
                e.stopPropagation();
                var itemPath = $(this).attr('rel');
                var obj = $(this).parent();
                objClicked = obj;
                folderInfo = itemPath;
                if (obj.children('ul').length > 0) {
                    obj.children('ul').slideToggle('1000', function () {
                        if (obj.parent('ul#subsub').length < 1) {
                            if (obj.children('ul').css('display') == 'none') {
                                objClicked = obj.parent().parent();
                                folderInfo = objClicked.children('a').attr('rel');
                            } else {
                                objClicked = obj;
                                folderInfo = objClicked.children('a').attr('rel');
                            }
                        } else {
                            if (obj.children('ul').css('display') == 'none') {
                                folderInfo = '/Uploads/images';
                            }
                        }
                    });
                    obj.toggleClass('folder-open');
                }
                bindClickCallAjax(e, itemPath, obj);
            });
            $('ul#subsub > li.file-image > a').on('click', function (e) {
                e.stopPropagation();
                $('#image-url').val($(this).attr('rel'));
                var cropImage = $('p.crop-image');
                var closeImage = $('img#close-image');
                $('p.crop-image img#show-image').attr('src', $(this).attr('rel'));
                cropImage.fadeIn();
                cropImage.mouseenter(function () {
                    closeImage.fadeIn();
                }).mouseleave(function () {
                    closeImage.fadeOut();
                });
                closeImage.click(function () {
                    $('#image-url').val('');
                    $('p.crop-image img#show-image').attr('src', '');
                    $(this).hide();
                    cropImage.fadeOut();
                });
            });
        },
        error: function (jqXHR, textStatus, exthrown) {
            alert(textStatus);
        }
    });
}
    
function bindClickCallAjax(e, itemPath, obj) {
    $('#image-url').val(itemPath);
    if (obj.children().length <= 1) {
        $.ajax({
            url: '/Product/LoadSubFolderStructure',
            type: 'POST',
            data: { itemPath: itemPath },
            success: function (data, textStatus) {
                var folderAppened = '<ul>';
                var fileAppened = '';
                if (data[0].length > 0) {
                    for (var i = 0; i < data[0].length; i++) {
                        folderAppened += '<li class="directory-collapsed isFolder"  style="margin-left: 0;"><a rel="/Uploads' + data[0][i] + '" href="#" class="key-pc">' + data[0][i].substring(data[0][i].lastIndexOf('/') + 1, data[0][i].length) + '</a></li>';
                    }
                }
                if (data[1].length > 0) {
                    for (var i = 0; i < data[1].length; i++) {
                        fileAppened += '<li class="file-image"  style="margin-left: 0;"><a rel="/Uploads' + data[1][i] + '" href="#">' + data[1][i].substring(data[1][i].lastIndexOf('/') + 1, data[1][i].length) + '</a></li>';
                    }
                }
                if (data[0].length > 0 || data[1].length > 0) {
                    obj.addClass("folder-open");
                    obj.append($(folderAppened + fileAppened + '</ul>').hide()).find('ul:last').slideDown();
                }
            },
            error: function (jqXHR, textStatus, ep) {
                alert(textStatus);
            },
            complete: function (jqXHR, textStatus) {
                obj.find('ul > li.isFolder > a').on('click', function (e) {
                    e.stopPropagation();
                    var itemPath = $(this).attr('rel');
                    var obj = $(this).parent();
                    objClicked = obj;
                    folderInfo = itemPath;
                    if (obj.children('ul').length > 0) {
                        obj.children('ul').slideToggle('1000', function () {
                            if (obj.parent('ul#subsub').length < 1) {
                                if (obj.children('ul').css('display') == 'none') {
                                    objClicked = obj.parent().parent();
                                    folderInfo = objClicked.children('a').attr('rel');
                                } else {
                                    objClicked = obj;
                                    folderInfo = objClicked.children('a').attr('rel');
                                }
                            } else {
                                if (obj.children('ul').css('display') == 'none') {
                                    folderInfo = '/Uploads/images';
                                }
                            }
                            console.log(objClicked);
                        });
                        obj.toggleClass('folder-open');
                    }
                    bindClickCallAjax(e, itemPath, obj);
                });
                obj.find('ul > li.file-image > a').on('click', function (e) {
                    e.stopPropagation();
                    $('#image-url').val($(this).attr('rel'));
                    var cropImage = $('p.crop-image');
                    var closeImage = $('img#close-image');
                    $('p.crop-image img#show-image').attr('src', $(this).attr('rel'));
                    cropImage.fadeIn();
                    cropImage.mouseenter(function () {
                        closeImage.fadeIn();
                    }).mouseleave(function () {
                        closeImage.fadeOut();
                    });
                    closeImage.click(function () {
                        $('#image-url').val('');
                        $('p.crop-image img#show-image').attr('src', '');
                        $(this).hide();
                        cropImage.fadeOut();
                    });
                });
            }
        });
    }
}



    function loadOrderFolder() {
        if (folderInfo != '/Uploads/images') {
            loadFolderStructoreTop();
            var url = objClicked.children('a').attr('rel');
            url = url.replace('/Uploads/images', '');
            var arr = url.split('/');
            var arrUrl = new Array(arr.length);
            var i, point = '';
            for (i = 0; i < arr.length; i++) {
                if (arr[i] != null && arr[i] != '') {
                    point += '/' + arr[i];
                    arrUrl[i] = '/Uploads/images' + point;
                }
                else {
                    arrUrl[i] = '/Uploads/images';
                }
            }
            for (i = 0; i < arrUrl.length; i++) {
                if (arrUrl[i] != '/Uploads/images') {
                    loadSubFolderSetTimeOut(arrUrl[i]);
                }
            }
        } else {
            loadFolderStructoreTop();
        }
    }
$('#create-folder').click(function () {
    if ($('#ipt-create-folder').val() != null && $('#ipt-create-folder').val() != '') {
        var val;
        if (folderInfo != '/Uploads/images') {
            loadFolderStructoreTop();
            var url = objClicked.children('a').attr('rel');
            url = url.replace('/Uploads/images', '');
            var arr = url.split('/');
            var arrUrl = new Array(arr.length);
            var i, point = '';
            for (i = 0; i < arr.length; i++) {
                if (arr[i] != null && arr[i] != '') {
                    point += '/' + arr[i];
                    arrUrl[i] = '/Uploads/images' + point;
                }
                else {
                    arrUrl[i] = '/Uploads/images';
                }
            }
            val = objClicked.children('a').attr('rel') + '/' + $('#ipt-create-folder').val();
            $.ajax({
                url: '/Product/CreateFolder',
                type: 'POST',
                data: { realFolder: val },
                async: false,
                success: function (data, textStatus) {
                },
                error: function (jqXHR, textStatus, ep) {
                    alert(textStatus);
                },
                complete: function (jqXHR, textStatus) {
                    for (i = 0; i < arrUrl.length; i++) {
                        if (arrUrl[i] != '/Uploads/images') {
                            loadSubFolderSetTimeOut(arrUrl[i]);
                        }
                    }
                }
            });

        } else {
            val = folderInfo + '/' + $('#ipt-create-folder').val();
            $.ajax({
                url: '/Product/CreateFolder',
                type: 'POST',
                data: { realFolder: val },
                success: function (data, textStatus) {
                },
                error: function (jqXHR, textStatus, ep) {
                    alert(textStatus);
                },
                complete: function (jqXHR, textStatus) {
                    loadFolderStructoreTop();
                }
            });
        }
    } else {
        $('span.no-input-folder').fadeIn();
    }
});
    
function loadSubFolderSetTimeOut(url) {
    setTimeout(function () {
        var realObj;
        $('#subsub li.isFolder a').each(function () {
            if ($(this).attr('rel') == url) {
                realObj = $(this).parent();
                return false;
            }
            return true;
        });
        $.ajax({
            url: '/Product/LoadSubFolderStructure',
            type: 'POST',
            data: { itemPath: url },
            async: false,
            success: function (data, textStatus) {
                var folderAppened = '<ul>';
                var fileAppened = '';
                var i;
                if (data[0].length > 0) {
                    for (i = 0; i < data[0].length; i++) {
                        folderAppened += '<li class="directory-collapsed isFolder"  style="margin-left: 0;"><a rel="/Uploads' + data[0][i] + '" href="#" class="key-pc">' + data[0][i].substring(data[0][i].lastIndexOf('/') + 1, data[0][i].length) + '</a></li>';
                    }
                }
                if (data[1].length > 0) {
                    for (i = 0; i < data[1].length; i++) {
                        fileAppened += '<li class="file-image"  style="margin-left: 0;"><a rel="/Uploads' + data[1][i] + '" href="#">' + data[1][i].substring(data[1][i].lastIndexOf('/') + 1, data[1][i].length) + '</a></li>';
                    }
                }
                if (data[0].length > 0 || data[1].length > 0) {
                    realObj.addClass("folder-open");
                    realObj.append($(folderAppened + fileAppened + '</ul>').hide()).find('ul:last').slideDown();
                }
            },
            error: function (jqXHR, textStatus, ep) {
                alert(textStatus);
            },
            complete: function (jqXHR, textStatus) {
                realObj.find('ul > li.isFolder > a').on('click', function (e) {
                    e.stopPropagation();
                    var itemPath = $(this).attr('rel');
                    var obj = $(this).parent();
                    objClicked = obj;
                    folderInfo = itemPath;
                    if (obj.children('ul').length > 0) {
                        obj.children('ul').slideToggle('1000', function () {
                            if (obj.parent('ul#subsub').length < 1) {
                                if (obj.children('ul').css('display') == 'none') {
                                    objClicked = obj.parent().parent();
                                    folderInfo = objClicked.children('a').attr('rel');
                                } else {
                                    objClicked = obj;
                                    folderInfo = objClicked.children('a').attr('rel');
                                }
                            } else {
                                if (obj.children('ul').css('display') == 'none') {
                                    folderInfo = '/Uploads/images';
                                }
                            }
                        });
                        obj.toggleClass('folder-open');
                    }
                    bindClickCallAjax(e, itemPath, obj);
                });

                realObj.find('ul > li.file-image > a').on('click', function (e) {
                    e.stopPropagation();
                    $('#image-url').val($(this).attr('rel'));
                    var cropImage = $('p.crop-image');
                    var closeImage = $('img#close-image');
                    $('p.crop-image img#show-image').attr('src', $(this).attr('rel'));
                    cropImage.fadeIn();
                    cropImage.mouseenter(function () {
                        closeImage.fadeIn();
                    }).mouseleave(function () {
                        closeImage.fadeOut();
                    });
                    closeImage.click(function () {
                        $('#image-url').val('');
                        $('p.crop-image img#show-image').attr('src', '');
                        $(this).hide();
                        cropImage.fadeOut();
                    });
                });
            }
        });

    }, 1000);
}

$('#ipt-create-folder').keypress(function() {
    $('span.no-input-folder').hide();
});

$('#select-image').click(function () {
    $('.folderUpload').val($('#image-url').val());
    $('#image-url').val('');
    $('#show-image').attr('src', '');
    $('.crop-image').hide();
    $('div.file-browser').fadeOut();
})